其他
服务管理和启动流程(1)-systemctl启动、查看服务
每周二、四、六定期更新,我们不见不散!
早期的centos使用systemV开机启动服务,CentOS7开始,改用systemd这个启动服务管理。
systemd的好处:
并行处理所有服务,加速开机 以前的init启动脚本是逐项依次启动的方式,没有依赖关系的服务之间也是逐项启动。这样的话就会拉长启动时间,现在服务器硬件、操作系统都支持并行,所以systemd可以让没有依赖关系的服务同时启动,缩短启动时间。 管理方便 systemd管理就只要一个systemd服务,通过systemctl这个命令进行操作,不会像systemV那样还需要init、chkconfig、service等相关命令。 自启动依赖服务 systemd会自动检查服务的依赖性并启动它。比如服务A依赖服务B,在启动服务A的时候服务B并没有启动,这个时候systemd会帮你自动把服务B启动起来。 向下兼容旧的init服务(即systemV开机启动服务)
使用systemctl管理服务
A
使用systemctl管理单一服务(service unit):自启动、启动、查看状态
[root@studyclub ~]# systemctl COMMAND [unit]
# COMMAND如下:
start 启动服务
restart 重启服务
stop 停止服务
reload 不关闭服务的情况下,重新加载服务的配置文件,让修改的配置文件立即生效
enable 让服务开机自启动
disable 关闭开机自启动
status 查看服务的当前运行状态:正在运行、已经停止运行等
is-active 目前有没有正在运行(基本不用)
is-enable 有没有配置这个服务的开机自启动
list-dependencies 查看本服务依赖哪些服务
示例:
1. 查看crond这个服务的状态
[root@studyclub ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-03-18 15:53:15 CST; 36s ago
Main PID: 56068 (nginx)
CGroup: /system.slice/nginx.service
├─56068 nginx: master process /usr/sbin/nginx
└─56069 nginx: worker process
Mar 18 15:53:15 studyclub systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 18 15:53:15 studyclub nginx[56063]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 18 15:53:15 studyclub nginx[56063]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 18 15:53:15 studyclub systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Mar 18 15:53:15 studyclub systemd[1]: Started The nginx HTTP and reverse proxy server.
2. 正常关闭服务
[root@studyclub ~]# systemctl stop nginx
[root@studyclub ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Thu 2021-03-18 15:55:36 CST; 3s ago
Main PID: 56068 (code=exited, status=0/SUCCESS)
Mar 18 15:53:15 studyclub systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 18 15:53:15 studyclub nginx[56063]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 18 15:53:15 studyclub nginx[56063]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 18 15:53:15 studyclub systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Mar 18 15:53:15 studyclub systemd[1]: Started The nginx HTTP and reverse proxy server.
Mar 18 15:55:36 studyclub systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Mar 18 15:55:36 studyclub systemd[1]: Stopped The nginx HTTP and reverse proxy server.
查看atd服务的状态:当前运行状态、设置的开机是否自启动
查看network服务的状态:当前运行状态、设置的开机是否自启动
请重启network服务
B
使用systemctl查看有哪些服务
[root@studyclub ~]# systemctl 【command】 [--type=TYPE] [--all]
选项:
list-units
list-unit-files
--type=TYPE : 主要有service、socket、target等
1. 列出所有的服务
[root@studyclub ~]# systemctl # 不加参数,默认参数就是list-units
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active running Arbitrary Executable File Formats File System Automount Poi
sys-devices-pci0000:00-0000:00:07.1-ata2-host1-target1:0:0-1:0:0:0-block-sr0.device loaded active plugged VMware_Virtual_IDE_CDROM_Drive
sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged VMware_Virtual_S 1
......
sysinit.target loaded active active System Initialization
timers.target loaded active active Timers
systemd-tmpfiles-clean.timer loaded active waiting Daily Cleanup of Temporary Directories
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
94 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
每一列的含义:
UNIT:服务的名称,同时可以根据服务名称的后缀看出这个服务的类别(type)
LOAD:开机时是否被加载
ACTIVE:目前的状态,和SUB列配合查看。就是我们systemctl status查看的Active那一行
DESCRIPTION:关于进程的描述
另外,systemctl不加参数,默认就是list-units
2. 列出所有已经安装的unit有哪些
[root@studyclub ~]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
......
time-sync.target static
timers.target static
umount.target static
fstrim.timer disabled
systemd-readahead-done.timer indirect
systemd-tmpfiles-clean.timer static
[root@studyclub ~]# systemctl list-units --type=service --all
# 这样就只剩下*.service的unit了
新手应知:
《Linux基础及进阶》:
看完本文有收获?请分享给更多人
推荐关注「Cloud研习社」,带你从零开始掌握云计算技术!
微信号|bjdream-1
Cloud研习社 ·